﻿/*    
Nemulation: A .NET based framework/platform for virtualization applications
Copyright (C) 2011 Bryan Perris

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ServiceModel;


namespace NEmuVirtualHost.Services
{
    public sealed class ServiceReflector : ICollection<Type>
    {
        private Assembly m_Assembly;
        private List<Type> m_ServiceTypes;

        public ServiceReflector(Assembly assembly)
        {
            m_ServiceTypes = new List<Type>();
            m_Assembly = assembly;
            FindServices();
        }

        public bool FindServices()
        {
            m_ServiceTypes.Clear();
            Type[] types = m_Assembly.GetTypes();

            foreach (Type type in types)
            {
                object[] attributes = type.GetCustomAttributes(typeof(ServiceBehaviorAttribute), true);

                if (attributes.Length > 0)
                {
                    m_ServiceTypes.Add(type);
                }
                else
                {
                    continue;
                }
            }

            if (m_ServiceTypes.Count > 0)
            {
                return true;
            }

            return false;
        }

        #region ICollection<> Memebers

        public void Add(Type item)
        {
            throw new NotSupportedException();
        }

        public void Clear()
        {
            throw new NotSupportedException();
        }

        public bool Contains(Type item)
        {
            return m_ServiceTypes.Contains(item);
        }

        public void CopyTo(Type[] array, int arrayIndex)
        {
            m_ServiceTypes.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return m_ServiceTypes.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(Type item)
        {
            throw new NotSupportedException();
        }

        public IEnumerator<Type> GetEnumerator()
        {
            return m_ServiceTypes.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return m_ServiceTypes.GetEnumerator();
        }

        #endregion
    }
}
